<?php
/**
 * 乐高页面保存指南内容接口，保存乐高id和内容
 */
namespace Frieza\Modules\Guide\Apis;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Doraemon\cache\Tag\TagGuideShare;
use Doraemon\tools\Checker;
use Doraemon\model\Guide as mGuide;
use Doraemon\tools\Filter;
use Doraemon\tools\ImgUrl;

class Savecontent extends Controller
{
    public function add(Request $request)
    {
        $params = $request->all();
        $guideId = $params['guide_id'] ?? '';
        $legoId = $params['lego_id'] ?? '';
        $content = $params['content'] ?? '';

        if (!Checker::isGuideid($guideId)) {     //验证指南ID
            return response()->json([
                'code' => 0,
                'data' =>[],
                'msg' => "请输入正确的id"
            ]);
        }
        $guide = mGuide::getGuide($guideId);

        if (empty($guide)) {
            return response()->json([
                'code' => 0,
                'data' =>[],
                'msg' => "指南不存在"
            ]);
        }

        if (empty($content)) {
            return response()->json([
                'code' => 0,
                'data' =>[],
                'msg' => "内容为空"
            ]);
        }

        if (!$legoId){
            return response()->json([
                'code' => 0,
                'data' =>[],
                'msg' => "乐高id不存在"
            ]);
        }

        $remark = Filter::getRemark($content);

        $save['lego_id'] = $legoId;
        $save['remark'] = $remark;
        $save['content'] = $content;
        $save['edittime'] = time();

        $res = mGuide::updateGuide($guideId, $save);

        if (!$res) {
            return response()->json([
                'code' => 0,
                'data' =>[],
                'msg' => "增加内容失败"
            ]);
        }

        $regex = "/<img.*?>/";
        preg_match($regex, $content, $match);
        preg_match("/img.+(data-photo-id=\"?\w*\").+/i", $match[0], $photo_id);
        $photoId = str_replace('"', '', trim($photo_id[1], "data-photo-id="));

        if (!empty($photoId)) {
            $share = TagGuideShare::getGuide($guideId);
            $shareArr = json_decode($share, true);

            $photo = \Doraemon\service\Photos::getPhoto($photoId);

            $upShare = false;
            if (!empty($share) && !empty($photo)) {
                foreach ($shareArr as  $item) {
                    foreach ($item as $k => &$val) {
                        if (!isset($val['cover_img']) || empty($val['cover_img'])) {
                            $upShare = true;
                            if ($k == 'weibo') {
                                $val['cover_img'] = $photo['pic_id'] ?? '';
                            } else {
                                $val['cover_img'] = ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $photo['pic_id'], ImgUrl::IMAGE_SIZE_SQ_THUMB);
                            }
                        }
                    }
                }
            }

            if ($upShare) {
                TagGuideShare::set($guideId, $shareArr);
            }
        }

        return response()->json([
            'code' => 1,
            'data'  =>[]
        ]);
    }
}